﻿using System;
using System.Collections;
using System.Text;
using UnityEngine;
using XUtliPoolLib;

namespace XUpdater
{
	// Token: 0x02000014 RID: 20
	public sealed class XDownloader : MonoBehaviour
	{
		// Token: 0x0600004E RID: 78 RVA: 0x000032E4 File Offset: 0x000014E4
		private void Awake()
		{
			this._progressCb = new XTimerMgr.ElapsedEventHandler(this.Progress);
		}

		// Token: 0x0600004F RID: 79 RVA: 0x000032F9 File Offset: 0x000014F9
		internal void GetBundle(XBundleData bundle, string url, HandleBundleDownload callback1, HandleFetchBundle callback2, float percent)
		{
			this._download = url.Contains("?token=");
			this._current_name = bundle.Name;
			this._total_percent = percent;
			base.StartCoroutine(this.Download(bundle, url, callback1, callback2));
		}

		// Token: 0x06000050 RID: 80 RVA: 0x00003333 File Offset: 0x00001533
		public void GetMeta(string url, string name, XDownloader.HandleBytesDownload callback, float percent)
		{
			this._download = true;
			this._current_name = name;
			this._total_percent = percent;
			base.StartCoroutine(this.MetaDownload(url, callback));
		}

		// Token: 0x06000051 RID: 81 RVA: 0x0000335B File Offset: 0x0000155B
		public void GetBytes(string url, XDownloader.HandleBytesDownload callback)
		{
			base.StartCoroutine(this.BytesDownload(url, callback));
		}

		// Token: 0x06000052 RID: 82 RVA: 0x0000336D File Offset: 0x0000156D
		private IEnumerator MetaDownload(string url, XDownloader.HandleBytesDownload callback)
		{
			XSingleton<XTimerMgr>.singleton.KillTimer(this._token);
			this._token = XSingleton<XTimerMgr>.singleton.SetTimer(0.1f, this._progressCb, null);
			this._downloader = new WWW(url);
			yield return this._downloader;
			XSingleton<XTimerMgr>.singleton.KillTimer(this._token);
			this.Progress(null);
			XSingleton<XTimerMgr>.singleton.KillTimer(this._token);
			if (callback != null)
			{
				callback(this._downloader, this._downloader.error);
			}
			this._downloader.Dispose();
			this._downloader = null;
			yield break;
		}

		// Token: 0x06000053 RID: 83 RVA: 0x0000338A File Offset: 0x0000158A
		private IEnumerator BytesDownload(string url, XDownloader.HandleBytesDownload callback)
		{
			WWW www = new WWW(url);
			yield return www;
			if (callback != null)
			{
				callback(www, www.error);
			}
			www.Dispose();
			www = null;
			yield break;
		}

		// Token: 0x06000054 RID: 84 RVA: 0x000033A7 File Offset: 0x000015A7
		private IEnumerator Download(XBundleData bundle, string url, HandleBundleDownload callback1, HandleFetchBundle callback2)
		{
			XSingleton<XTimerMgr>.singleton.KillTimer(this._token);
			this._token = XSingleton<XTimerMgr>.singleton.SetTimer(0.1f, this._progressCb, null);
			this._downloader = new WWW(url);
			yield return this._downloader;
			XSingleton<XTimerMgr>.singleton.KillTimer(this._token);
			this.Progress(null);
			XSingleton<XTimerMgr>.singleton.KillTimer(this._token);
			bool error = false;
			if (callback1 != null)
			{
				AsyncWriteRequest awr = callback1(this._downloader, bundle, this._downloader.error);
				while (!awr.IsDone)
				{
					if (awr.HasError)
					{
						error = true;
						break;
					}
					yield return null;
				}
				XSingleton<XUpdater>.singleton.XPlatform.SetNoBackupFlag(awr.Location);
				awr = null;
			}
			if (error)
			{
				this._log.Length = 0;
				this._log.AppendFormat(XSingleton<XStringTable>.singleton.GetString("XUPDATE_ERROR_DOWNLOADRESFAILED"), bundle.Name);
				XSingleton<XLoadingUI>.singleton.SetStatus(this._log.ToString(), byte.MaxValue, byte.MaxValue, byte.MaxValue);
			}
			else
			{
				if (callback2 != null)
				{
					callback2(this._downloader, this._downloader.bytes, bundle, this._download);
				}
			}
			this._downloader = null;
			yield break;
		}

		// Token: 0x06000055 RID: 85 RVA: 0x000033D4 File Offset: 0x000015D4
		private void Progress(object o)
		{
			this._log.Remove(0, this._log.Length);
			int num = Mathf.FloorToInt(this._total_percent * 100f);
			if (this._download)
			{
				this._log.AppendFormat(XSingleton<XStringTable>.singleton.GetString("XUPDATE_INFO_DOWNLOADING"), num);
			}
			else
			{
				this._log.AppendFormat(XSingleton<XStringTable>.singleton.GetString("XUPDATE_INFO_EXTRACTING"), num);
			}
			XSingleton<XLoadingUI>.singleton.SetStatus(this._log.ToString(), byte.MaxValue, byte.MaxValue, byte.MaxValue);
			this._token = XSingleton<XTimerMgr>.singleton.SetTimer(0.1f, this._progressCb, o);
		}

		// Token: 0x0400003E RID: 62
		private StringBuilder _log = new StringBuilder();

		// Token: 0x0400003F RID: 63
		private WWW _downloader = null;

		// Token: 0x04000040 RID: 64
		private bool _download = true;

		// Token: 0x04000041 RID: 65
		private uint _token = 0U;

		// Token: 0x04000042 RID: 66
		private float _total_percent = 0f;

		// Token: 0x04000043 RID: 67
		private string _current_name = null;

		// Token: 0x04000044 RID: 68
		private XTimerMgr.ElapsedEventHandler _progressCb = null;

		// Token: 0x02000280 RID: 640
		// (Invoke) Token: 0x06000D7F RID: 3455
		public delegate void HandleBytesDownload(WWW www, string error);
	}
}
